<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="merchantId">
        <el-select v-model="queryParams.merchantId" style="width:220px;" placeholder="请选择商户"
                   @keyup.enter.native="handleQuery">
          <el-option key="0" value="0" label="独立店铺"></el-option>
          <el-option v-for="mer in merchantList"
                     :key="mer.merchantId"
                     :label="mer.defindName ? mer.defindName : mer.merchantName"
                     :value="mer.merchantId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增授权
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <span class="text-danger">双击修改备注信息</span>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="tableData">
      <el-table-column label="商户Id" v-if="false" align="center" prop="merchantId"/>
      <el-table-column label="商户" width="180" align="center" prop="merchantName"/>
      <el-table-column label="店铺">
        <template slot-scope="scope">
          <div class="shops-contain">
            <el-card v-for="shop in scope.row.shops" class="shop-card" shadow="hover" @dblclick.native="handleUpdate(shop)">
              <div slot="header">
                <span class="text">{{ shop.defindName ? shop.defindName : shop.shopName }}</span>
                <el-switch style="float: right; padding: 3px 0" v-model="shop.isPublish"
                           @change="updateIsPublish(shop)"></el-switch>
              </div>
              <div>
                ID:{{ shop.shopId }}
                &nbsp;<el-tag v-if="shop.isSip" size="mini" type="success">SIP</el-tag>
                &nbsp;<el-tag v-if="!shop.auth" size="mini" type="danger">授权失败</el-tag>
              </div>
              <div class="item">
                <el-tag>{{ shop.region }}</el-tag>
                &nbsp;
                <span>{{shop.remark }}</span>
              </div>

            </el-card>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="别名" prop="defindName">
              <el-input v-show="false" v-model="form.shopId"></el-input>
              <el-input v-model="form.defindName" placeholder="请输入别名"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listAllMerchant} from "@/api/shop/merchant";
import {
  getAuthUrl, getShop,
  listShop,
  updateShop,
} from "@/api/shop/shop";
import Cookies from "js-cookie";

export default {
  name: "Shop",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 选中数组
      ids: [],
      //全球商户列表
      merchantList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        merchantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // merchantName: [{required: true, message: "不能为空", trigger: "blur"}],
      },
    };
  },
  created() {
    this.getMerchantList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //处理商户选中
    getMerchantList() {
      listAllMerchant().then((response) => {
        this.merchantList = response.data;
        //默认选中
        if (Cookies.get('merchantId')) {
          this.queryParams.merchantId = +Cookies.get('merchantId');
        }
        this.getList();
      });
    },
    //更新是否发布
    updateIsPublish(data) {
      let {shopId, isPublish} = data;
      updateShop({shopId, isPublish}).then((response) => {
        this.$message({
          type: "success",
          message: "状态更新成功",
        });
      });

    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.queryParams.merchantId && this.queryParams.merchantId !=='0') {
        Cookies.set('merchantId', this.queryParams.merchantId);
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      getAuthUrl().then((response) => {
        window.open(response.msg, "_blank");
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.shopId || this.ids;
      getShop(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺《"+response.data.shopName+"》的备注信息";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shopId: null,
        defindName: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.shopId != null) {
            updateShop(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
//@import '@/assets/styles/variables.scss';

.shops-contain {
  display: flex;
  flex-wrap: wrap;

  .shop-card {
    width: 260px;
    margin: 8px;
  }

  .text {
    font-size: 18px;
    white-space: nowrap;

    //font-weight: bold;
  }

  .item {
    margin-top: 5px;

    .el-tag {
      height: 40px;
      line-height: 40px;
      font-size: 30px;
    }
  }
}

</style>
